﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace WeishaLearningSystemRegester
{
    public partial class FormMain : Form
    {
        string _pattern = @"^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$";

        public FormMain()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                var type = (cboActivateType.SelectedItem as ActiveTypeItem).ActivationType;
                var generator = new LicenseGenerator(new GenerateConfig
                {
                    Domain = txtDomain.Text,
                    PlatformName = "study",
                    PlatformVersion = txtVersion.Text,
                    Port = (int)numPort.Value,
                    CPU = txtDomain.Text,
                    HardDisk = txtDomain.Text,
                });
                var desKey = generator.GetDecryptKey(type);
                var retStr = generator.EncryptForDES($"0;{txtDomain.Text}:{numPort.Value};{dt1.Value:yyyy-MM-dd};{dt2.Value:yyyy-MM-dd};6;6", desKey);
                var ret = generator.EncryptForAscii($"{(int)type}" + retStr);

                var lic = $@"起始时间：{dt1.Value:yyyy年MM月dd日}；
结束时间：{dt2.Value:yyyy年MM月dd日}；
版本等级：jianmo；
授权类型：jianmo；
授权对象：jianmo；
=" + ret;
                var rx = generator._initLocalLicense(lic);

                if (rx)
                {
                    System.IO.File.WriteAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "license.weisha"), lic);
                    MessageBox.Show("授权文件已经生成");
                }
            }
            catch { }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            cboActivateType.Items.Add(new ActiveTypeItem { ActivationType = ActivationType.CPU, Caption = "CPU" });
            cboActivateType.Items.Add(new ActiveTypeItem { ActivationType = ActivationType.HardDisk, Caption = "硬盘" });
            cboActivateType.Items.Add(new ActiveTypeItem { ActivationType = ActivationType.IP, Caption = "IP" });
            cboActivateType.Items.Add(new ActiveTypeItem { ActivationType = ActivationType.Domain, Caption = "域名" });
            cboActivateType.Items.Add(new ActiveTypeItem { ActivationType = ActivationType.Root, Caption = "根域名" });
            cboActivateType.SelectedIndex = 0;
            lblPort.Visible = false;
            numPort.Visible = false;

            cboActivateType.SelectedIndexChanged += CboActivateType_SelectedIndexChanged;
        }

        private void CboActivateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboActivateType.SelectedIndex > 1)
            {
                lblPort.Visible = true;
                numPort.Visible = true;
            }
            else
            {
                lblPort.Visible = false;
                numPort.Visible = false;
            }
        }
    }

    class ActiveTypeItem
    {
        public ActivationType ActivationType { get; set; }

        public string Caption { get; set; }

        public override string ToString()
        {
            return Caption;
        }
    }
}
